﻿@{
    ViewBag.Title = "首页商品列表";
}
@section CSS{

}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            首页商品
            <small>列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">营销管理</a></li>
            <li class="active">首页商品列表</li>
        </ol>
    </section>
    <section>
        <div class="col-xs-12">
            <div class="box box-primary ">
                <!-- form start -->
                <form class="form-horizontal">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-2">
                                <select class="form-control" id="group_status">
                                    <option value="-1">-请选择状态-</option>
                                    <option value="0">禁用</option>
                                    <option value="1">启用</option>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label">添加时间</label>
                            <div class="col-sm-2">
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input type="text" class="form-control pull-right" id="start_date">
                                </div>
                            </div>
                            <label style="float:left;"> - </label>
                            <div class="col-sm-2">
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input type="text" class="form-control pull-right" id="end_date">
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer ">
                        <button type="button" class="btn btn-primary btn-search center-block" id="btnSearch"><i class="fa fa-search"></i> 查询</button>
                        <button type="button" class="btn btn-info" id="btnAdd" onclick="window.location.href = '/Marketing/HomeGoodsBatchAdd'"><i class="fa fa-plus"></i> 添加首页商品</button>
                    </div>
                    <!-- /.box-footer -->
                </form>
            </div>
        </div>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    @*<div class="box-header">
                            &nbsp;
                        </div>*@
                    <!-- /.box-header -->
                    <div class="box-body">
                        <table id="DataList" class="table table-bordered table-striped table-hover ">
                            <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>图片</th>
                                    <th>商品名称</th>
                                    <th>商品编号</th>
                                    <th>排序</th>
                                    <th>添加时间</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody> </tbody>
                            @*<tfoot>
                                    <tr>
                                    <th>编号</th>
                                    <th>图片</th>
                                    <th>商品名称</th>
                                    <th>商品编号</th>
                                    <th>状态</th>
                                    <th>排序</th>
                                    <th>添加时间</th>
                                    <th>操作</th>
                                </tr>
                                </tfoot>*@
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->
@section JS{
    <script>
        $(function () {
            //初始化表格
            GenerateTable();
            //鼠标移动到该行变色
            $('.table-hover tbody').on('click', 'tr', function () {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                }
                else {
                    u_table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
            });
            //搜索事件
            $("#btnSearch").on("click", function () {
                var start_date = $('#start_date').val();
                var end_date = $("#end_date").val();
                if ((start_date == null && end_date != null) || (start_date != null && end_date == null)) {
                    layer.msg('请把时间填充完整!', { icon: 1, time: 1000 });
                }
                else {
                    u_table.fnDestroy();
                    GenerateTable();
                }
            });

            $('#start_date').datepicker({
                autoclose: true
            });

            $('#end_date').datepicker({
                autoclose: true
            });
        });
        //生成表格
        function GenerateTable() {
            u_table = $('#DataList').dataTable({
                "lengthChange": false,
                "searching": false,
                "ordering": false,
                "iDisplayLength": 10,
                "bProcessing": true,
                "bServerSide": true,
                "sScrollX": true,
                "bAutoWidth":false,
                "fnServerParams": function (aoData) {
                    var start_date = $('#start_date').val();
                    var end_date = $("#end_date").val();
                    var group_status = $("#group_status").val();
                    aoData.push(
                        { "name": "start_date", "value": start_date },
                        { "name": "end_date", "value": end_date },
                        { "name": "status", "value": group_status }
                    );
                },
                "sAjaxSource": "/Marketing/GetHomeGoods",
                "aoColumns": [
                    { "mDataProp": 'home_goods_id' },
                    {
                        "mDataProp": 'picture_url', "mRender": function (data, type, full) {
                            return '<img src=' +domain+ data + ' style="width:80px; height:80px"  />';
                        }
                    },
                    { "mDataProp": 'goods_name' },
                    { "mDataProp": 'goods_id' },
                    { "mDataProp": 'sort' },
                    {
                        "mDataProp": 'add_time', "mRender": function (data, type, full) {
                            return moment(data).format("YYYY-MM-DD HH:mm:ss");
                        }
                    },
                    {
                        "mDataProp": 'status', "mRender": function (data, type, full) {
                            if (data) {
                                return '<a style="text-decoration:none" class="btn btn-success" onClick="user_noPass(this,' + full.home_goods_id + ')" href="javascript:;" title="点击设为启用">启用</a>';
                            } else {
                                return '<a style="text-decoration:none" class="btn btn-danger" onClick="user_pass(this,' + full.home_goods_id + ')" href="javascript:;" title="点击设为禁用">禁用</a>';
                            }
                        }
                    },
                    {
                        "mDataProp": 'home_goods_id', "mRender": function (data, type, full) {
                            return '<a class="btn btn-default " href = "/Marketing/HomeGoodsDetail/' + data + '"  title="编辑">编辑</a> '
                        }
                    }
                ]
            });
        }

        /*启用*/
        function user_pass(obj, id) {
            layer.confirm('确定启用？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/Marketing/UpdateHomeGoodsStatus",
                    dataType: "json",
                    data: { home_goods_id: id, status: 1 },
                    success: function (res) {
                        if (res.data == true) {
                            $(obj).text("启用").attr("onClick", "user_noPass(this," + id + ")").attr("title", "点击设为启用").attr("class", "btn btn-success");
                            layer.msg('已启用', { icon: 6, time: 1000 });
                        }
                        else {
                            alert(res.msg);
                        }
                    }
                });
            });
        }
        /*禁用*/
        function user_noPass(obj, id) {
            layer.confirm('确定禁用？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/Marketing/UpdateHomeGoodsStatus",
                    dataType: "json",
                    data: { home_goods_id: id, status: 0 },
                    success: function (res) {
                        if (res.data == true) {
                            $(obj).text("禁用").attr("onClick", "user_pass(this," + id + ")").attr("title", "点击设为禁用").attr("class", "btn btn-danger");
                            layer.msg('已禁用', { icon: 5, time: 1000 });
                        }
                        else {
                            alert(res.msg);
                        }
                    }
                });

            });
        }
    </script>
}